package com.uairobot.bill.tts.agent.utils;

import java.io.File;
import java.net.URL;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PathKitUtil {
  /**
   * 返回静态目录的真实文件夹
   */
  private static String realResourcePath = null;

  public static String getResourcePath() {
    if (realResourcePath == null) {
      String key = PropKitUtil.get("undertow.resourcePath");
      String[] split = key.split(":");
      if (split.length > 1) {
        String folderName = split[1];
        log.info("folderName : " + folderName);
        if (split[0].equals("file")) {
          return folderName;
        } else {
          if (realResourcePath == null) {
            URL resource = PathKitUtil.class.getClassLoader().getResource(folderName);
            try{
              realResourcePath = resource.getFile();	
            }catch(Exception e){
              //创建文件夹
            	resource=mkdirsInClasspath(folderName);
            	realResourcePath = resource.getFile();
            }
            
          }
        }
      }
    }

    return realResourcePath;
  }

private static URL mkdirsInClasspath(String folderName) {
	URL resource = PathKitUtil.class.getClassLoader().getResource("");
	File file = new File(resource.getFile()+folderName);
	if(!file.exists()){
		file.mkdirs();
		log.info("mkdirs:"+file.getAbsolutePath());
	}
	resource = PathKitUtil.class.getClassLoader().getResource(folderName);
	return resource;
}
}